<template>
	<view>
		<ly-curriculumtable :weekTableState="true" :weekTableNumber="weekNumbers" :controlWeek="controlWeek"
			:timetableType="timetableType" :timetables="timetables" :startdDate="currentDate" :weekButton="weekButton"
			@nextWeekClick="nextWeekClick" @lastWeekClick="lastWeekClick" @courseClick="courseClick"
			@weekSelectClick="weekSelectClick" @weekOpenClick="weekOpenClick">

		</ly-curriculumtable>
		<view style="margin: 15px 13px 20px;border: 2px solid skyblue">
			<text>“其他”是指 1.时间接近或者超过三个\n小时的日程 2. 对应位置已有日程的日程 3.时间待定的日程</text>
		</view>
		<view style="padding: 10px;">
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				/* 目前用户id */
				uid: 1,
				apiUrl: "http://localhost:8080/",
				/* 月日历 */
				currentDate: '',
				/* 当前日期 */

				/* 周 */
				weekSchedules: [],
				weekNumbers: 1, //一共显示几周
				controlWeek: 1, //显示的第几周
				weekButton: false, //开启上一周下一周按钮
				timetables: [
					['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '',
						'', ''
					],
					['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '',
						'', ''
					],
					['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '',
						'', ''
					],
					['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '',
						'', ''
					],
					['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '',
						'', ''
					],
					['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '',
						'', ''
					],
					['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '',
						'', ''
					],
				],
				timetablesId: [
					['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '',
						'', ''
					],
					['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '',
						'', ''
					],
					['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '',
						'', ''
					],
					['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '',
						'', ''
					],
					['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '',
						'', ''
					],
					['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '',
						'', ''
					],
					['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '',
						'', ''
					],
				],
				timeTableBeginTime: [
					['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '',
						'', ''
					],
					['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '',
						'', ''
					],
					['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '',
						'', ''
					],
					['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '',
						'', ''
					],
					['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '',
						'', ''
					],
					['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '',
						'', ''
					],
					['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '',
						'', ''
					],
				],
				timeTableEndTime: [
					['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '',
						'', ''
					],
					['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '',
						'', ''
					],
					['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '',
						'', ''
					],
					['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '',
						'', ''
					],
					['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '',
						'', ''
					],
					['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '',
						'', ''
					],
					['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '',
						'', ''
					],
				],
				timeTableExtraTime: [
					[],
					[],
					[],
					[],
					[],
					[],
					[]
				],
				timeTableDayTime: [
					[],
					[],
					[],
					[],
					[],
					[],
					[]
				],
				timetableType: [{
						index: '0',
						name: '全天'
					},
					{
						index: '1',
						name: '00:00\n01:00'
					},
					{
						index: '2',
						name: '01:00\n02:00'
					},
					{
						index: '3',
						name: '02:00\n03:00'
					},
					{
						index: '4',
						name: '03:00\n04:00'
					},
					{
						index: '5',
						name: '04:00\n05:00'
					},
					{
						index: '6',
						name: '05:00\n06:00'
					},
					{
						index: '7',
						name: '06:00\n07:00'
					},
					{
						index: '8',
						name: '07:00\n08:00'
					},
					{
						index: '9',
						name: '08:00\n09:00'
					},
					{
						index: '10',
						name: '09:00\n10:00'
					},
					{
						index: '11',
						name: '10:00\n11:00'
					},
					{
						index: '12',
						name: '11:00\n12:00'
					},
					{
						index: '13',
						name: '12:00\n13:00'
					},
					{
						index: '14',
						name: '13:00\n14:00'
					},
					{
						index: '15',
						name: '14:00\n15:00'
					},
					{
						index: '16',
						name: '15:00\n16:00'
					},
					{
						index: '17',
						name: '16:00\n17:00'
					},
					{
						index: '18',
						name: '17:00\n18:00'
					},
					{
						index: '19',
						name: '18:00\n19:00'
					},
					{
						index: '20',
						name: '19:00\n20:00'
					},
					{
						index: '21',
						name: '20:00\n21:00'
					},
					{
						index: '22',
						name: '21:00\n22:00'
					},
					{
						index: '23',
						name: '22:00\n23:00'
					},
					{
						index: '24',
						name: '23:00\n24:00'
					},
					{
						index: '25',
						name: '其他'
					},
				],
				weekIndex: 0,
			}
		},
		onLoad() {
			/* 数据传值 */
			this.uid = uni.getStorageSync("uid")
			console.log('onload')
		},
		onReady() {

		},
		created() {
			console.log('created')
			this.initData()
		},
		onShow() {
			/* 刷新 */
			console.log('onShow')
			this.clear();
			this.showWeekData()
		},
		onPullDownRefresh() { //延迟一秒刷新成功
			this.clear();
			this.showWeekData()
			console.log('刷新'),
				setTimeout(() => {
					this.msg = 'refresh',
						uni.stopPullDownRefresh()
				}, 1000)

		},
		methods: {
			/* 周 */
			courseClick(re) {
				console.log('点击了', re)
				switch (re.week) {
					case '一':
						this.weekIndex = 0;
						break;
					case '二':
						this.weekIndex = 1;
						break;
					case '三':
						this.weekIndex = 2;
						break;
					case '四':
						this.weekIndex = 3;
						break;
					case '五':
						this.weekIndex = 4;
						break;
					case '六':
						this.weekIndex = 5;
						break;
					case '七':
						this.weekIndex = 6;
						break;
				}
				var index = re.index - 1;
				var itemList = [];
				var itemId = [];
				if (index == 0) {
					console.log('全天日程', this.timeTableDayTime[this.weekIndex])
					for (var i = 0; i < this.timeTableDayTime[this.weekIndex].length; i++) {
						var id = this.timeTableDayTime[this.weekIndex][i].id;
						var title = this.timeTableDayTime[this.weekIndex][i].title;
						var beginTime = this.timeTableDayTime[this.weekIndex][i].beginTime;
						var endTime = this.timeTableDayTime[this.weekIndex][i].endTime;
						var item = '标题：' + title + '，时间：' + beginTime + '-' + endTime
						itemList.push(item)
						itemId.push(id)
						console.log(id, title, beginTime, endTime)
					}
				} else if (index == 25) {
					console.log('其他日程', this.timeTableExtraTime[this.weekIndex])
					for (var i = 0; i < this.timeTableExtraTime[this.weekIndex].length; i++) {
						var id = this.timeTableExtraTime[this.weekIndex][i].id;
						var title = this.timeTableExtraTime[this.weekIndex][i].title;
						var beginTime = this.timeTableExtraTime[this.weekIndex][i].beginTime;
						var endTime = this.timeTableExtraTime[this.weekIndex][i].endTime;
						var item = '标题：' + title + '，时间：' + beginTime + '-' + endTime
						itemList.push(item)
						itemId.push(id)
						console.log(id, title, beginTime, endTime)
					}
				} else {
					var id = this.timetablesId[this.weekIndex][index];
					var title = this.timetables[this.weekIndex][index];
					var beginTime = this.timeTableBeginTime[this.weekIndex][index];
					var endTime = this.timeTableEndTime[this.weekIndex][index];
					var item = '标题：' + title + '，时间：' + beginTime + '-' + endTime
					itemList.push(item)
					itemId.push(id)
					console.log(id, title, beginTime, endTime)
				}

				if (id != "") {
					uni.showActionSheet({
						itemList: itemList,
						success(res) {
							console.log(res.tapIndex)
							var id = itemId[res.tapIndex]
							uni.navigateTo({
								url: '/pages/mine/checkSchedule?id=' + id
							})
						},
						fail(res) {
							// 取消后的操作
						}
					})
				}
			},
			nextWeekClick(e) {
				console.log("下一周", e)
			},
			lastWeekClick(e) {
				console.log("上一周", e)
			},
			weekOpenClick() {
				console.log("点击了第几周")
			},
			weekSelectClick(e) {
				console.log("您选择了", e)
			},
			//关闭弹窗
			closeModal() {
				this.showMyModal = this.showMyModal == true ? false : true;
			},
			//返回颜色
			colorList() {
				return [
					"#ffffff", //0
					"#ffaa00", //1
					"#33CC99", //2
					"#ff5500", //3
					"#789262", //4
					"#66CCCC", //5
					"#9999FF", //6
				]
			},
			processWeekData(weekSchedules) {
				console.log("处理周数据", weekSchedules);
				var count = 0;
				var extraCount = 1;
				var weekCount = 0;
				var dayCount = 1;
				for (let key in weekSchedules) {
					console.log("属性：" + key + ",值：" + weekSchedules[key]);
					if (weekSchedules[key] != "") {
						var weekSchedule = weekSchedules[key];
						console.log("属性：" + key + "不为空");
						for (var i = 0; i < weekSchedule.length; i++) {
							var id = weekSchedule[i].id
							var title = weekSchedule[i].title
							var beginTime = weekSchedule[i].beginTime
							var endTime = weekSchedule[i].endTime
							if (beginTime == '时间待定' ||endTime=='时间待定') {
								this.timetablesId[weekCount][25] = this.timetablesId[weekCount][25] + id + '/';
								console.log('对应id', this.timetablesId[weekCount][25])
								this.timetables[weekCount][25] = "此处\n" + extraCount + "\n个日程";
								extraCount++;
								this.timeTableExtraTime[weekCount].push({
									"id": id,
									"title": title,
									"beginTime": beginTime,
									"endTime": endTime
								})
							}
							var beginNum = beginTime.substring(0, 2)
							var endNum = endTime.substring(0, 2)
							console.log('开始时间和结束时间相差', Number(endNum) - Number(beginNum))
							if (Number(endNum) - Number(beginNum) == 24) {
								/* 全天放第一个格子 */
								console.log('开始时间和结束时间相差', '=24')
								this.timetablesId[weekCount][0] = this.timetablesId[weekCount][0] + id + '/';
								console.log('对应id', this.timetablesId[weekCount][0])
								this.timetables[weekCount][0] = "此处\n" + dayCount + "\n个日程";
								dayCount++;
								this.timeTableDayTime[weekCount].push({
									"id": id,
									"title": title,
									"beginTime": beginTime,
									"endTime": endTime
								})
							} else if (Number(endNum) - Number(beginNum) > 3) {
								/* 大于3放最后一个格子 */
								console.log('开始时间和结束时间相差', '>3')
								this.timetablesId[weekCount][25] = this.timetablesId[weekCount][25] + id + '/';
								console.log('对应id', this.timetablesId[weekCount][25])
								this.timetables[weekCount][25] = "此处\n" + extraCount + "\n个日程";
								extraCount++;
								this.timeTableExtraTime[weekCount].push({
									"id": id,
									"title": title,
									"beginTime": beginTime,
									"endTime": endTime
								})
							} else {
								console.log('开始时间和结束时间相差', '<3')
								var isExtra = 0;
								for (var j = 0; j <= Number(endNum) - Number(beginNum); j++) {
									if (this.timetables[weekCount][Number(beginNum) + 1 + j] == '') {
										continue;
									} else {
										isExtra = 1;
										break;
									}
								}
								if (isExtra == 1) {
									this.timetablesId[weekCount][25] = this.timetablesId[weekCount][25] + id +
										'/'; /* 格子不为空放最后一个格子 */
									console.log('对应id', this.timetablesId[weekCount][25])
									this.timetables[weekCount][25] = "此处\n" + extraCount + "\n个日程";
									console.log('对应title', this.timetables[weekCount][25])
									extraCount++;
									this.timeTableExtraTime[weekCount].push({
										"id": id,
										"title": title,
										"beginTime": beginTime,
										"endTime": endTime
									})
								} else {
									for (var j = 0; j <= Number(endNum) - Number(beginNum); j++) {
										this.timetablesId[weekCount][Number(beginNum) + 1 + j] = id;
										console.log('对应id', this.timetablesId[weekCount][Number(beginNum) + 1 + j])
										this.timetables[weekCount][Number(beginNum) + 1 + j] = title;
										console.log('对应title', this.timetables[weekCount][Number(beginNum) + 1 + j])
										this.timeTableBeginTime[weekCount][Number(beginNum) + 1 + j] = beginTime;
										this.timeTableEndTime[weekCount][Number(beginNum) + 1 + j] = endTime;
									}
								}
							}
						}
						console.log(this.timetables)
					}
					dayCount = 1;
					weekCount++;
					count++;
					extraCount = 1;
				};
			},
			showWeekData() {
				console.log('请求“周”数据', this.currentDate)
				var weekSchedules = {};
				var weekSchedule = [];
				uni.request({
					url: this.apiUrl + 'schedule/getWeek',
					method: 'GET',
					data: {
						uid: this.uid,
						date: this.currentDate
					},
					header: {
						Authorization:"Bearer " + uni.getStorageSync('token')
					},
					success: (res) => {
						console.log('请求远程周数据')
						weekSchedules = res.data;
						console.log("周日程", weekSchedules);
						uni.setStorageSync(this.uid + 'weekSchedule' + this.currentDate, weekSchedules)
						this.processWeekData(weekSchedules)

					},
					fail: (err) => {

						console.log('请求本地周数据')
						weekSchedules = uni.getStorageSync(this.uid + 'weekSchedule' + this.currentDate);
						console.log("周日程", weekSchedules);
						this.processWeekData(weekSchedules)
					}
				});
			},
			initData() {
				console.log('获得当前日期')
				var now = new Date();
				var year = now.getFullYear(); //得到年份
				var month = now.getMonth(); //得到月份
				var date = now.getDate(); //得到日期
				month = month + 1;
				if (month < 10) month = "0" + month;
				if (date < 10) date = "0" + date;
				this.currentDate = year + "-" + month + "-" + date;
				console.log("当前日期", this.currentDate);
			},
			clear() {
				this.weekSchedules = [],
					this.weekNumbers = 1; //一共显示几周
				this.controlWeek = 1; //显示的第几周
				this.weekButton = false; //开启上一周下一周按钮
				this.timetables = [
					['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '',
						'', ''
					],
					['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '',
						'', ''
					],
					['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '',
						'', ''
					],
					['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '',
						'', ''
					],
					['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '',
						'', ''
					],
					['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '',
						'', ''
					],
					['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '',
						'', ''
					],
				];
				this.timetablesId = [
					['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '',
						'', ''
					],
					['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '',
						'', ''
					],
					['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '',
						'', ''
					],
					['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '',
						'', ''
					],
					['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '',
						'', ''
					],
					['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '',
						'', ''
					],
					['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '',
						'', ''
					],
				];
				this.timeTableBeginTime = [
					['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '',
						'', ''
					],
					['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '',
						'', ''
					],
					['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '',
						'', ''
					],
					['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '',
						'', ''
					],
					['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '',
						'', ''
					],
					['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '',
						'', ''
					],
					['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '',
						'', ''
					],
				];
				this.timeTableEndTime = [
					['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '',
						'', ''
					],
					['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '',
						'', ''
					],
					['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '',
						'', ''
					],
					['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '',
						'', ''
					],
					['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '',
						'', ''
					],
					['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '',
						'', ''
					],
					['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '',
						'', ''
					],
				];
				this.timeTableExtraTime = [
					[],
					[],
					[],
					[],
					[],
					[],
					[]
				];
				this.timeTableDayTime = [
					[],
					[],
					[],
					[],
					[],
					[],
					[]
				];
				this.timetableType = [{
						index: '0',
						name: '全天'
					},
					{
						index: '1',
						name: '00:00\n01:00'
					},
					{
						index: '2',
						name: '01:00\n02:00'
					},
					{
						index: '3',
						name: '02:00\n03:00'
					},
					{
						index: '4',
						name: '03:00\n04:00'
					},
					{
						index: '5',
						name: '04:00\n05:00'
					},
					{
						index: '6',
						name: '05:00\n06:00'
					},
					{
						index: '7',
						name: '06:00\n07:00'
					},
					{
						index: '8',
						name: '07:00\n08:00'
					},
					{
						index: '9',
						name: '08:00\n09:00'
					},
					{
						index: '10',
						name: '09:00\n10:00'
					},
					{
						index: '11',
						name: '10:00\n11:00'
					},
					{
						index: '12',
						name: '11:00\n12:00'
					},
					{
						index: '13',
						name: '12:00\n13:00'
					},
					{
						index: '14',
						name: '13:00\n14:00'
					},
					{
						index: '15',
						name: '14:00\n15:00'
					},
					{
						index: '16',
						name: '15:00\n16:00'
					},
					{
						index: '17',
						name: '16:00\n17:00'
					},
					{
						index: '18',
						name: '17:00\n18:00'
					},
					{
						index: '19',
						name: '18:00\n19:00'
					},
					{
						index: '20',
						name: '19:00\n20:00'
					},
					{
						index: '21',
						name: '20:00\n21:00'
					},
					{
						index: '22',
						name: '21:00\n22:00'
					},
					{
						index: '23',
						name: '22:00\n23:00'
					},
					{
						index: '24',
						name: '23:00\n24:00'
					},
					{
						index: '25',
						name: '其他'
					},
				];
				this.weekIndex = 0;
			},
		}
	}
</script>

<style>
</style>
